package com.lzh.others.collection.Map;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Description: map学习
 * @Author: LiuZiHao
 * @Date: 2022/4/28 14:28
 */
public class MapMain {

    public static void main(String[] args) {
        /* --- 1 ---*/
        /* keySet() 返回此映射中所包含的键的set视图，集合受到映射支持， 通过iterator.remove,set.remove removeAll 和 Clear操作
         * 从映射移除相应的映射关系。不支持add 或 addAll 操作*/
//        Map map = new HashMap(10);
//        map.put(1,"a");
//        map.put(2,"b");
//        map.put(3,"c");
//        map.put(4,"d");
//        Set key1 = map.keySet();
//        Set key2 = map.keySet();
//        Set key3 = map.keySet();
//        key1.remove(1);

//        System.out.println(key1);
//        System.out.println(key2);
//        System.out.println(key3);

        /* --- 2 ---*/
//        Map map2 = new HashMap(10);
//        map2.put("key1", "value1");
//        map2.put("key2", "value2");
//        map2.put("key3", "value3");
//        String[] keySet = (String[]) map2.keySet().toArray(new String[0]);
//        for (String key : keySet) {
//            map2.remove(key);
//        }

        /* --- 3 ---*/
        System.out.println("------> Map的4种遍历 <------");
        Map<String, String> map3 = new HashMap<String, String>();
        map3.put("1", "value1");
        map3.put("2", "value2");
        map3.put("3", "value3");
        //遍历 1 通过Map.keySet 遍历 key 和 value
        System.out.println("===============================");
        for (String key : map3.keySet()) {
            System.out.println("key = " + key + " and value = " + map3.get(key));
        }
        //2 通过Map.entrySet 使用 iterator 遍历 key 和 value
        System.out.println("===============================");
        Iterator<Map.Entry<String, String>> iterator = map3.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }
        //3 推荐 尤其是容量大时
        System.out.println("===============================");
        for (Map.Entry<String, String> entry : map3.entrySet()) {
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }
        //4 通过Map.values() 遍历所有的value 不能遍历key
        System.out.println("===============================");
        for (String value : map3.values()) {
            System.out.println("value = " + value);
        }

        int i = 1 / 3600;
        System.out.println("i = " + i);


    }
}
